package com.gxan.service.impl;

import com.gxan.common.JsonResult;
import com.gxan.common.R;
import com.gxan.dao.RecruitmentDao;
import com.gxan.dao.impl.RecruitmentDaoImpl;
import com.gxan.pojo.Recruitment;
import com.gxan.service.RecruitmentService;

import java.sql.SQLException;
import java.util.List;

public class RecruitmentServiceImpl implements RecruitmentService {
    private static RecruitmentDao recruitmentDao = new RecruitmentDaoImpl();
    @Override
    public R AddRecruitment(Recruitment recruitment) {
        //创建返回前端需要的封装类
        R<Object> objectR = new R<>();


        int i = 0;
        try {
            i = recruitmentDao.addRecruitment(recruitment);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        if(i>0){
            objectR.setCode(200);
            objectR.setMsg("添加成功");
        }else{
            objectR.setCode(500);
            objectR.setMsg("添加失败");
        }
        return objectR;
    }

    @Override
    public JsonResult queryRecruitmentList(Integer currentPage, Integer pageSize,Recruitment recruitment) {
        //创建返回的对象JsonResult
        JsonResult<List<Recruitment>> jsonResult = new JsonResult<>();
        //查询需要的JsonResult内容
        //查询总条数和list<Recruitment>
        try {
            Integer integer = recruitmentDao.countRecruitment(recruitment);
            List<Recruitment> employees = recruitmentDao.queryRecruitmentList((currentPage-1)*pageSize, pageSize, recruitment);
            jsonResult.setCode(0);
            jsonResult.setCount(integer);
            jsonResult.setMsg("查询职位发布成功");
            jsonResult.setData(employees);

        } catch (SQLException e) {
            jsonResult.setCode(500);
            jsonResult.setMsg("服务器出问题");
            e.printStackTrace();
        }
        return jsonResult;
    }

    @Override
    public R delRecruitmentById(String[] ids) {
        //创建返回的对象R
        R<Object> objectR = new R<>();
        //添加R需要的数据
        for (String id : ids) {
            try {
                int i = recruitmentDao.delRecruitmentById(id);
                if(i>0){
                    objectR.setCode(200);
                    objectR.setMsg("删除员工成功");
                }
            } catch (SQLException e) {
                objectR.setCode(500);
                objectR.setMsg("服务器问题");
                e.printStackTrace();
            }
        }

        return objectR;
    }

    @Override
    public R updateRecruitment(Recruitment recruitment) {
        //创建返回前端需要的封装类
        R<Object> objectR = new R<>();


        int i = -1;
        try {
            i = recruitmentDao.updateRecruitment(recruitment);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        if(i>=0){
            objectR.setCode(200);
            objectR.setMsg("添加成功");
        }else{
            objectR.setCode(500);
            objectR.setMsg("添加失败");
        }
        return objectR;
    }
}
